package com.itedge.solutionmanager.web.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itedge.infrastructure.service.domain.IProcessEntityService;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.solutionmanager.constants.SolutionManagerConstants;
import com.itedge.solutionmanager.domain.impl.Solution;
import com.itedge.solutionmanager.taskdata.solution.inbox.SolutionTaskInboxData;

@RequestMapping("/solutionTaskInbox")
@Controller
public class SolutionTaskInboxController {	

    @Autowired
    private IProcessService<Solution> solutionProcessService;
    
    @Autowired
    private IProcessEntityService<Solution> solutionService;

	@Autowired
	private ReloadableResourceBundleMessageSource messageSource;
    
	@RequestMapping(params = "searchForm", method = RequestMethod.GET)
    public String searchForm(Model model) {
        return "solutionTaskInbox/search";
    }    
    
	/*
	 * Method used for AJAX async search requests on inbox tasks, returning data in JSON format
	 */
	@RequestMapping(params = "searchForm", method = RequestMethod.POST, headers = "Accept=application/json")
    public @ResponseBody List<SolutionTaskInboxData> searchAsyncForInboxTasks(@RequestParam(value = "searchName") String searchName,
    		@RequestParam(value = "maxResults", required = false) Integer maxResults, HttpServletRequest request) {			
		Locale currentLocale = request.getLocale();
		List<Task> taskInboxList = solutionProcessService.readPendingTasksByName('%'+searchName.toUpperCase()+'%',maxResults);
        return constructInboxDataList(taskInboxList, currentLocale);
    }    
    
	@RequestMapping(method = RequestMethod.GET)
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size,
    		Model model, HttpServletRequest request) {
		Locale currentLocale = request.getLocale();
		if (page != null || size != null) {
			int sizeNo = size == null ? SolutionManagerConstants.DEFAULT_SIZE_PER_PAGE : size.intValue();
			List<Task> tasks = solutionProcessService.readPendingTasks(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo);
			model.addAttribute("inboxDataList", constructInboxDataList(tasks, currentLocale));
            float nrOfPages = (float) solutionProcessService.countAllPendingTasks() / sizeNo;
            model.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
		} else {
			List<Task> allTasks = solutionProcessService.readAllPendingTasks();
			model.addAttribute("inboxDataList", constructInboxDataList(allTasks, currentLocale));			
		}
		return "solutionTaskInbox/list";
	}
    
	public List<SolutionTaskInboxData> constructInboxDataList(List<Task> tasks, Locale currentLocale) {
		List<SolutionTaskInboxData> inboxList = new ArrayList<SolutionTaskInboxData>();
		Iterator<Task> taskIt = tasks.iterator();
		while(taskIt.hasNext()) {
			Task taskRecord = taskIt.next();
			Solution linkedSolution = solutionService.findEntityByProcessId(taskRecord.getProcessInstanceId());
			SolutionTaskInboxData inboxData = new SolutionTaskInboxData(linkedSolution, taskRecord, messageSource, currentLocale);
			inboxList.add(inboxData);
		}
		return inboxList;
	}
}
